package com.xky.sportstestdemo01.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xky.sportstestdemo01.common.ScoreCalculator;
import com.xky.sportstestdemo01.mapper.RePhysicalFitnessMapper;
import com.xky.sportstestdemo01.mapper.RePhysicalFitnessScoresMapper;
import com.xky.sportstestdemo01.mapper.StudentMapper;
import com.xky.sportstestdemo01.pojo.entity.RePhysicalFitness;
import com.xky.sportstestdemo01.pojo.entity.RePhysicalFitnessScores;
import com.xky.sportstestdemo01.pojo.entity.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 体测成绩表副本(RePhysicalFitness)表服务实现类
 *
 * @author twt
 * @since 2024-11-08 13:50:08
 */

/**
 * 体测成绩表副本(RePhysicalFitness)表服务实现类
 */
@Service("rePhysicalFitnessService")
public class RePhysicalFitnessService extends ServiceImpl<RePhysicalFitnessMapper, RePhysicalFitness> {

    @Autowired
    private RePhysicalFitnessMapper rePhysicalFitnessMapper;

    @Autowired
    private RePhysicalFitnessScoresMapper rePhysicalFitnessScoresMapper;

    @Autowired
    private StudentMapper studentMapper;

    /**
     * @auther: 唐文涛
     * 根据学生id判断性别
     */
    public boolean isMale(Integer studentId) {
        // 查询Student表中的数据
        Student student = studentMapper.selectById(studentId);
        //TODO——触发警报
        if (student == null) {
            return false;
        }
        return student.getGender() == 1;
    }

    /**
     * @auther: 唐文涛
     * 根据学生id判断大一大二还是大三大四
     */
    public boolean isOneTwo(Integer studentId) {
        // 查询Student表中的数据
        Student student = studentMapper.selectById(studentId);
        //TODO——触发警报
        if (student == null) {
            return false;
        }
        return student.getGradeNumber().equals("41") || student.getGradeNumber().equals("42");
    }

    public RePhysicalFitnessScores calculateScores(Integer studentId) {
        // 查询rephysical_fitness表中的数据
        RePhysicalFitness rePhysicalFitness = rePhysicalFitnessMapper.selectById(studentId);
        if (rePhysicalFitness == null) {
            return null;
        }

        RePhysicalFitnessScores scores = new RePhysicalFitnessScores();
        scores.setReStudentId(rePhysicalFitness.getReStudentId());

        // 使用ScoreCalculator计算各项成绩
        scores.setReLungCapacityScore(ScoreCalculator.calculateLungCapacityScore(rePhysicalFitness.getReLungCapacity(), isMale(studentId), isOneTwo(studentId)));
        scores.setReFiftyMeterRunScore(ScoreCalculator.calculateFiftyMeterRunScore(rePhysicalFitness.getReFiftyMeterRun(), isMale(studentId), isOneTwo(studentId)));
        scores.setReStandingLongJumpScore(ScoreCalculator.calculateStandingLongJumpScore(rePhysicalFitness.getReStandingLongJump(), isMale(studentId), isOneTwo(studentId)));
        scores.setReSitAndReachScore(ScoreCalculator.calculateSitAndReachScore(rePhysicalFitness.getReSitAndReach(), isMale(studentId), isOneTwo(studentId)));
        scores.setReEightHundredMeterRunScore(ScoreCalculator.calculateEightHundredMeterRunScore(rePhysicalFitness.getReEightHundredMeterRun(), isMale(studentId), isOneTwo(studentId)));
        //TODO 时间换算
        scores.setReOneThousandMeterRunScore(ScoreCalculator.calculateOneThousandMeterRunScore(rePhysicalFitness.getReOneThousandMeterRun(), isMale(studentId), isOneTwo(studentId)));
        scores.setReOneMinuteSitUpsScore(ScoreCalculator.calculateOneMinuteSitUpsScore(rePhysicalFitness.getReOneMinuteSitUps(), isMale(studentId), isOneTwo(studentId)));
        scores.setRePullUpsScore(ScoreCalculator.calculatePullUpsScore(rePhysicalFitness.getRePullUps(), isMale(studentId), isOneTwo(studentId)));

        // 计算总分
        double totalScore = scores.getReLungCapacityScore() +
                scores.getReFiftyMeterRunScore() +
                scores.getReStandingLongJumpScore() +
                scores.getReSitAndReachScore() +
                scores.getReEightHundredMeterRunScore() +
                scores.getReOneThousandMeterRunScore() +
                scores.getReOneMinuteSitUpsScore() +
                scores.getRePullUpsScore();

        scores.setReTotalScore(totalScore);

        rePhysicalFitnessScoresMapper.insert(scores);

        return scores;
    }
}


